package wyq.gy;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVGeoPoint;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.SignUpCallback;
import com.bigkoo.pickerview.TimePickerView;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import me.leefeng.citypicker.CityPicker;
import me.leefeng.citypicker.CityPickerListener;
import wyq.gy.lib.ActivityList;

public class CompleteInfoActivity extends AppCompatActivity implements CityPickerListener{

    private String m_address;
    private double m_latitude;
    private double m_longitude;
    private EditText m_complete_name;
    private EditText m_complete_identity;
    private TextView m_complete_poi;
    private TextView m_complete_birthday;
    private TextView m_complete_birth_place;
    private EditText m_complete_self_intro;
    private String m_username;
    private String m_password;
    private EditText m_company;
    private TimePickerView pvTime;
    private CityPicker cityPicker1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_complete_info);
        ActivityList.getInstance().addActivity(this);
        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        if(actionBar != null){
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        m_username = getIntent().getStringExtra("username");
        m_password = getIntent().getStringExtra("userPwd");
        initTimePicker();
        Init();
    }
    private void Init(){
        m_complete_name = (EditText) findViewById(R.id.comlete_name);
        m_complete_identity = (EditText) findViewById(R.id.comlete_identity);
        m_complete_birthday = (TextView) findViewById(R.id.comlete_birthday);
        m_complete_birthday.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(pvTime != null) {
                    pvTime.show(v);
                }
            }
        });
        m_complete_birth_place = (TextView) findViewById(R.id.comlete_birth_place);
        cityPicker1 = new CityPicker(CompleteInfoActivity.this, this);
        m_complete_birth_place.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                cityPicker1.show();
            }
        });
//        m_complete_self_intro = (EditText) findViewById(R.id.comlete_self_intro);
        m_company = (EditText)findViewById(R.id.comlete_company_name);

        m_complete_poi = (TextView) findViewById(R.id.select_poi);
        m_complete_poi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(CompleteInfoActivity.this, GetPositionActivity.class);
                startActivityForResult(intent,10);
            }
        });

        Button btn = (Button)findViewById(R.id.info_done_btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (m_complete_name.getText().length() == 0 || m_complete_identity.getText().length() == 0 || m_complete_poi.getText().length() == 0) {
                    Toast.makeText(CompleteInfoActivity.this, "请完善基本信息", Toast.LENGTH_LONG).show();
                } else {
                    RegUser();
            }
        }
        });
    }

    private void RegUser(){
        AVUser user = new AVUser();// 新建 AVUser 对象实例
        user.setUsername(m_username);// 设置用户名
        user.setPassword(m_password);// 设置密码
        user.put("Name", m_complete_name.getText());
        user.put("IDNumber", m_complete_identity.getText());
          user.put("DateBirth",m_complete_birthday.getText());
         user.put("JiGuan",m_complete_birth_place.getText());
        //    user.put("selfintro",m_complete_self_intro.getText());
        AVGeoPoint point = new AVGeoPoint(m_latitude, m_longitude);
        user.put("RegCoordinate", point);
        user.put("RegLocation", m_complete_poi.getText());
        user.put("IdMark", 1);
        user.put("CompanyName",m_company.getText());
        user.signUpInBackground(new SignUpCallback() {
            @Override
            public void done(AVException e) {
                if (e == null) {
                    Toast.makeText(CompleteInfoActivity.this, "注册成功", Toast.LENGTH_LONG).show();
                    Intent intent = new Intent();
                    intent.setClass(CompleteInfoActivity.this, MainActivity.class);
                    startActivity(intent);
                    ActivityList.getInstance().FinishAll();
                } else {
                    Toast.makeText(CompleteInfoActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    @Override
    public void getCity(final String name) {
        if (cityPicker1.isShow()) {
            m_complete_birth_place.setText(name);
            return;
        }
    }

    private void initTimePicker() {
        //控制时间范围(如果不设置范围，则使用默认时间1900-2100年，此段代码可注释)
        //因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
        Calendar selectedDate = Calendar.getInstance();
        Calendar startDate = Calendar.getInstance();
        startDate.set(1900, 0, 23);
        Calendar endDate = Calendar.getInstance();
        endDate.set(2100, 11, 28);
        pvTime = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                TextView btn = (TextView) v;
                btn.setText(getTime(date));
            }
        })
            //年月日时分秒 的显示与否，不设置则默认全部显示
            .setType(new boolean[]{true,true, true, false, false, false})
            .setLabel("", "", "", "点", "分", "")
            .isCenterLabel(false)
            .setDividerColor(Color.DKGRAY)
            .setContentSize(21)
            .setDate(selectedDate)
            .setRangDate(startDate, endDate)
            .setBackgroundId(0x00FFFFFF) //设置外部遮罩颜色
            .setDecorView(null)
            .build();
    }

    private String getTime(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");//("yyyy-MM-dd")
        return format.format(date);
    }

    // 回调的方式来获取指定Activity返回的结果
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode==10 && resultCode==10){
            if(data != null) {
                Bundle bundle = data.getBundleExtra("addr");
                m_address = bundle.getString("address");
                m_latitude = bundle.getDouble("latitude");
                m_longitude = bundle.getDouble("longitude");
                m_complete_poi.setText(m_address);
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish(); // back button
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
